package com.sys.gradepaysys.dao;

import com.sys.gradepaysys.entity.*;
import com.sys.gradepaysys.utils.DBUtil;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseDao {
    /**
     * 通用 删除 修改  添加操作
     *
     * @param sql sql语句
     * @param arr 可变参数数组
     */
    public static boolean DoObject(String sql, String... arr) {
        Connection connection = DBUtil.getConnection();
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < arr.length; i++) {
                preparedStatement.setString(i + 1, arr[i]);
            }
            boolean execute = preparedStatement.execute();
            if (!execute) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.release(connection, preparedStatement);
        }
    }

    /**
     * 通用查询数据以及封装
     *
     * @param sql sql语句
     * @param arr 可变数组参数
     * @return
     */
    public static List<Map<String, String>> selectList(String sql, String... arr) {
        Connection connection = DBUtil.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ResultSetMetaData metaData = null;
        List<Map<String, String>> dataList = new ArrayList<>();
        Map<String, String> userMap = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < arr.length; i++) {
                preparedStatement.setString(i + 1, arr[i]);
            }
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                userMap = new HashMap<>();
                metaData = resultSet.getMetaData();
                for (int i = 0; i < metaData.getColumnCount(); i++) {
                    userMap.put(metaData.getColumnName(i + 1), resultSet.getString(i + 1));
                }
                dataList.add(userMap);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.release(connection, preparedStatement);
        }
        return dataList;
    }

    public static List<Student> selectStuList(String sql, String... arr) {
        List<Map<String, String>> maps = selectList(sql, arr);
        List<Student> students = new ArrayList<>();
        for (Map<String, String> map : maps) {
            Student student = new Student();
            if (map.get("s_id") != null) {
                student.setsId(Integer.valueOf(map.get("s_id")));
            }
            if (map.get("s_name") != null) {
                student.setsName(map.get("s_name"));
            }
            if (map.get("password") != null) {
                student.setPassword(map.get("password"));
            }
            if (map.get("sex") != null) {
                student.setSex(map.get("sex"));
            }
            if (map.get("age") != null) {
                student.setAge(Integer.valueOf(map.get("age")));
            }
            if (map.get("qq") != null) {
                student.setQq(map.get("qq"));
            }
            if (map.get("s_tel") != null) {
                student.setsTel(map.get("s_tel"));
            }
            if (map.get("g_name") != null) {
                student.setsTel(map.get("g_name"));
            }
            if (map.get("c_name") != null) {
                student.setsTel(map.get("c_name"));
            }
            students.add(student);
        }
        return students;
    }

    public static List<Admin> selectAdminList(String sql, String... arr) {
        List<Map<String, String>> maps = selectList(sql, arr);
        List<Admin> admins = new ArrayList<>();
        for (Map<String, String> map : maps) {
            Admin admin = new Admin();
            if (map.get("aid") != null) {
                admin.setAid(Integer.valueOf(map.get("aid")));
            }
            if (map.get("username") != null) {
                admin.setUsername(map.get("username"));
            }
            if (map.get("password") != null) {
                admin.setPassword(map.get("password"));
            }
            admins.add(admin);
        }
        return admins;
    }

    public static List<Grade> selectGradeList() {
        String sql = "select * from grade";
        List<Map<String, String>> maps = selectList(sql);
        List<Grade> grades = new ArrayList<>();
        for (Map<String, String> map : maps) {
            Grade grade = new Grade();
            if (map.get("g_id") != null) {
                grade.setGid(Integer.valueOf(map.get("g_id")));
            }
            if (map.get("g_name") != null) {
                grade.setGname(map.get("g_name"));
            }
            grades.add(grade);
        }
        return grades;
    }
    public static List<Clazz> selectClazzList() {
        String sql = "select * from clazz";
        List<Map<String, String>> maps = selectList(sql);
        List<Clazz> clazzes = new ArrayList<>();
        for (Map<String, String> map : maps) {
            Clazz clazz=new Clazz();
            if (map.get("c_id") != null) {
                clazz.setCid(Integer.valueOf(map.get("c_id")));
            }
            if (map.get("c_name") != null) {
               clazz.setcName(map.get("c_name"));
            }

            if (map.get("g_id") != null) {
               clazz.setGid(Integer.valueOf(map.get("g_id")));
            }
            clazzes.add(clazz);
        }
        return clazzes;
    }
    public static List<Clazz> selectClazzByGid(String gid,String cName) {
        String sql = "select * from clazz where g_id=? and c_name=?";
        String arr[]={gid,cName};
        List<Map<String, String>> maps = selectList(sql,arr);
        List<Clazz> clazzes = new ArrayList<>();
        for (Map<String, String> map : maps) {
            Clazz clazz=new Clazz();
            if (map.get("c_id") != null) {
                clazz.setCid(Integer.valueOf(map.get("c_id")));
            }
            if (map.get("c_name") != null) {
                clazz.setcName(map.get("c_name"));
            }

            if (map.get("g_id") != null) {
                clazz.setGid(Integer.valueOf(map.get("g_id")));
            }
            clazzes.add(clazz);
        }
        return clazzes;
    }
    public static List<Event> selectEventList() throws ParseException {
        String sql = "select * from event where is_doing =1";
        List<Map<String, String>> maps = selectList(sql);
        List<Event> events = new ArrayList<>();
        for (Map<String, String> map : maps) {
            Event event=new Event();
            if (map.get("e_id") != null) {
               event.setEid(Integer.valueOf(map.get("e_id")));
            }
            if (map.get("is_doing") != null) {
                event.setIsDoing(Integer.valueOf(map.get("is_doing")));
            }
            if (map.get("e_pay") != null) {
                event.setePay(Double.valueOf(map.get("e_pay")));
            }

            if (map.get("e_name") != null) {
               event.seteName(map.get("e_name"));
            }
            if (map.get("e_num") != null) {
                 event.seteNum(Integer.valueOf(map.get("e_num")));
            }

            if (map.get("s_date") != null) {
                event.setsTime(map.get("s_date"));
            }
            if (map.get("e_date") != null) {
                 event.seteTime(map.get("e_date"));
            }
            if (map.get("is_ok") != null) {
                event.setIsOk(Integer.valueOf(map.get("is_ok")));
            }

            if (map.get("sup_num") != null) {
                event.setSupNum(Integer.valueOf(map.get("sup_num")));
            }
            if (map.get("no_sup") != null) {
                event.setNoSup(Integer.valueOf(map.get("no_sup")));
            }
            if (map.get("e_total") != null) {
                 event.setTotalPay(Double.valueOf(map.get("e_total")));
            }
            if (map.get("c_id") != null) {
                event.setCid(Integer.valueOf(map.get("c_id")));
            }
           events.add(event);
        }
        return events;
    }
    public static List<Event> selectEventList2() throws ParseException {
        String sql = "select * from event where is_doing =0";
        List<Map<String, String>> maps = selectList(sql);
        List<Event> events = new ArrayList<>();
        for (Map<String, String> map : maps) {
            Event event=new Event();
            if (map.get("e_id") != null) {
                event.setEid(Integer.valueOf(map.get("e_id")));
            }
            if (map.get("is_doing") != null) {
                event.setIsDoing(Integer.valueOf(map.get("is_doing")));
            }
            if (map.get("e_pay") != null) {
                event.setePay(Double.valueOf(map.get("e_pay")));
            }

            if (map.get("e_name") != null) {
                event.seteName(map.get("e_name"));
            }
            if (map.get("e_num") != null) {
                event.seteNum(Integer.valueOf(map.get("e_num")));
            }

            if (map.get("s_date") != null) {
                event.setsTime(map.get("s_date"));
            }
            if (map.get("e_date") != null) {
                event.seteTime(map.get("e_date"));
            }
            if (map.get("is_ok") != null) {
                event.setIsOk(Integer.valueOf(map.get("is_ok")));
            }

            if (map.get("sup_num") != null) {
                event.setSupNum(Integer.valueOf(map.get("sup_num")));
            }
            if (map.get("no_sup") != null) {
                event.setNoSup(Integer.valueOf(map.get("no_sup")));
            }
            if (map.get("e_total") != null) {
                event.setTotalPay(Double.valueOf(map.get("e_total")));
            }
            if (map.get("c_id") != null) {
                event.setCid(Integer.valueOf(map.get("c_id")));
            }
            events.add(event);
        }
        return events;
    }
    public static List<Event> selectEventIsDoingList(String sql,String ...arr) throws ParseException {
        List<Map<String, String>> maps = selectList(sql,arr);
        List<Event> events = new ArrayList<>();
        for (Map<String, String> map : maps) {
            Event event=new Event();
            if (map.get("e_id") != null) {
                event.setEid(Integer.valueOf(map.get("e_id")));
            }
            if (map.get("e_pay") != null) {
                event.setePay(Double.valueOf(map.get("e_pay")));
            }
            if (map.get("is_doing") != null) {
                event.setIsDoing(Integer.valueOf(map.get("is_doing")));
            }
            if (map.get("e_name") != null) {
                event.seteName(map.get("e_name"));
            }
            if (map.get("e_num") != null) {
                event.seteNum(Integer.valueOf(map.get("e_num")));
            }

            if (map.get("s_date") != null) {
                event.setsTime(map.get("s_date"));
            }
            if (map.get("e_date") != null) {
                event.seteTime(map.get("e_date"));
            }
            if (map.get("is_ok") != null) {
                event.setIsOk(Integer.valueOf(map.get("is_ok")));
            }

            if (map.get("sup_num") != null) {
                event.setSupNum(Integer.valueOf(map.get("sup_num")));
            }
            if (map.get("no_sup") != null) {
                event.setNoSup(Integer.valueOf(map.get("no_sup")));
            }
            if (map.get("e_total") != null) {
                event.setTotalPay(Double.valueOf(map.get("e_total")));
            }
            if (map.get("c_id") != null) {
                event.setCid(Integer.valueOf(map.get("c_id")));
            }
            events.add(event);
        }
        return events;
    }

}
